package cn.example.broadcastbestpractice;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class LoginActivity extends BaseActivity{
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    private CheckBox checkBox;
    SharedPreferences spf;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        accountEdit = findViewById(R.id.account);
        passwordEdit = findViewById(R.id.password);
        login = findViewById(R.id.login);
        checkBox = findViewById(R.id.remember_pwd);
        spf = getSharedPreferences("data", MODE_PRIVATE);
        accountEdit.setText(spf.getString("account", ""));
        passwordEdit.setText(spf.getString("password", ""));
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                if("admin".equals(account) && "123456".equals(password)){
                    if(checkBox.isChecked()){
                        SharedPreferences.Editor editor = spf.edit();
                        editor.putString("account", account);
                        editor.putString("password", password);
                        editor.apply();
                    }
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    Toast.makeText(LoginActivity.this, "account or password is valid", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
